package com.eff.android.lib.connection;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.eff.android.api.model.Model;
import com.eff.android.config.EConfig;
import com.eff.android.config.EConstant;
import com.eff.android.lib.asynctask.JsonParser;
import com.eff.android.lib.helper.EDatabaseHelper;

public class EDataConnection {

	public final String TAG = EDataConnection.class.getSimpleName();
    
    // contacts JSONArray
    JSONArray dataJsonArr = null;
    
    // Database Helper
 	EDatabaseHelper db;
 	EDatabaseHelper dbHelper;
 		
 	private Context mContext;
 	//List<Model> listModels;
 	
 	public EDataConnection(Context context) {
        super();
        mContext = context;
    }
    
    public String doInBackground() {

    	// Create List Model
        List<Model> listModels = new ArrayList<Model>();
        
        try {
            
            // instantiate our json parser
            JsonParser jParser = new JsonParser();                              
            
            // get json string from url
            //JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
            dataJsonArr = jParser.getJSONFromUrl(EConfig.yourJsonStringUrl);
            
            // get the array of users
            //dataJsonArr = json.getJSONArray(null);
           
            // loop through all users
            for (int i = 0; i < dataJsonArr.length(); i++) {

                JSONObject c = dataJsonArr.getJSONObject(i);

                // Storing each json item in variable
                String id_hinh = c.getString(EConstant.KEY_ID_HINH);
                String van_ban = c.getString(EConstant.KEY_VAN_BAN);
                String y_nghia = c.getString(EConstant.KEY_Y_NGHIA);
                String uri_hinh = c.getString(EConstant.KEY_URI_HINH);
                String phien_am_quoc_te = c.getString(EConstant.KEY_PHIEN_AM_QUOC_TE);
                String loai_hinh = c.getString(EConstant.KEY_LOAI_HINH);
                String level = c.getString(EConstant.KEY_LEVEL);
                String ma_chu_de = c.getString(EConstant.KEY_MA_CHU_DE);               
                String date_create = c.getString(EConstant.KEY_DATE_CREATE);
                
             // Create Model
                Model tempModel = new Model(id_hinh, van_ban, y_nghia, uri_hinh, phien_am_quoc_te,
                                loai_hinh, level, ma_chu_de, date_create);
                
                //Add Model to List
                listModels.add(tempModel);
                 
                
                // show the values in our logcat
                Log.e(TAG, "id_hinh: " + tempModel.getIdHinh() 
                        + ", van_ban: " + tempModel.getVanBan()
                        + ", y_nghia: " + tempModel.getYNghia()
                        + ", uri_hinh: " + tempModel.getUriHinh()
                        + ", phien_am_quoc_te: " + tempModel.getPhienAmQuocTe()
                        + ", loai_hinh: " + tempModel.getLoaiHinh()
                        + ", level: " + tempModel.getLevel()
                        + ", ma_chu_de: " + tempModel.getMaChuDe()
                        + ", date_create: " + tempModel.getDateCreate());
            }
                
            //Log.d("Pass JSON : ", String.valueOf(listModes.size()));
        } catch (JSONException e) {
            e.printStackTrace();
        }               
    
        try {
            
            db = new EDatabaseHelper(mContext);
            
            // Inserting models in database
                    
                    for (int i = 0; i < listModels.size(); i++)
                    {
                            
                            Log.e(TAG, "id_hinh: " + listModels.get(i).getIdHinh() 
                        + ", van_ban: " + listModels.get(i).getVanBan()
                        + ", y_nghia: " + listModels.get(i).getYNghia()
                        + ", uri_hinh: " + listModels.get(i).getUriHinh()
                        + ", phien_am_quoc_te: " + listModels.get(i).getPhienAmQuocTe()
                        + ", loai_hinh: " + listModels.get(i).getLoaiHinh()
                        + ", level: " + listModels.get(i).getLevel()
                        + ", ma_chu_de: " + listModels.get(i).getMaChuDe()
                        + ", date_create: " + listModels.get(i).getDateCreate());
                            db.createModel(listModels.get(i));
                    }
                    
            Log.e("Model Count", "Model count: " + db.getModelCount());
                    
            // Test Getting all Models
            Log.d("Get Models", "Getting All Models");
            
//            List<Model> allModels = db.getAllModels(10);
//            for (Model model : allModels) {
//                Log.d("Model", model.getVanBan());
//            }       
            
            /* // Test Getting Single Models
            Model model = new Model();
            model = db.getModel("147200");
            Log.d("Single Model : ", model.getIdHinh());
            */
            
            /*
            // Test Deleting a Model
            Log.d("Delete Model", "Deleting a Model");
            Log.d("Tag Count", "Tag Count Before Deleting: " + db.getModelCount());
     
            List<Model> allModels = db.getAllModels();
            for (int i = 0; i < db.getModelCount(); i++)
            {
                    db.deleteModel(allModels.get(i).getIdHinh());
            }
     
            Log.d("Tag Count", "Tag Count After Deleting: " + db.getModelCount());
            */
            
            // Don't forget to close database connection
            db.closeDB();
            
            } catch (SQLiteException e) {
            e.printStackTrace();
        }        
    
        return null;
    }
}
